package com.wing.cabinet.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.cabinet.dao.CellSingleInventoryProductDao;
import com.wing.cabinet.model.entity.CabAlarmSettings;
import com.wing.cabinet.model.entity.CellSingleInventoryProduct;
import com.wing.cabinet.model.request.CellSingleInventoryProductForm;
import com.wing.cabinet.model.response.CellSingleInventoryProductExportVO;
import com.wing.cabinet.model.response.CellSingleInventoryProductVO;
import com.wing.cabinet.model.response.CelllnventoryAlarmExportVO;
import com.wing.order.model.entity.CabInventoryProduct;
import com.wing.order.service.CabInventoryProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 售刀柜sku库存(某个柜子)-ServiceImpl
 *
 * @author
 */
@Service
@Slf4j
public class CellSingleInventoryProductServiceImpl extends ServiceImpl<CellSingleInventoryProductDao, CellSingleInventoryProduct> implements CellSingleInventoryProductService {

    @Autowired
    private CabAlarmSettingsServiceImpl alarmSettingsService;

    @Autowired
    private CabInventoryProductService inventoryProductService;

    @Override
    public int add(CellSingleInventoryProductForm cellSingleInventoryProductForm) {

        //校验

        CellSingleInventoryProduct cellSingleInventoryProduct =new CellSingleInventoryProduct();
        CopyOptions copyOptions = CopyOptions.create()
        .setEditable(CellSingleInventoryProduct.class)
        .setIgnoreError(true)
        .setIgnoreNullValue(true);
        BeanUtil.copyProperties(cellSingleInventoryProductForm, cellSingleInventoryProduct, copyOptions);

        return baseMapper.insert(cellSingleInventoryProduct);
    }

    @Override
    public List<CellSingleInventoryProductVO> list4app(Map<String, Object> params) {
        return baseMapper.list4app(params);
    }

    @Override
    public List<CellSingleInventoryProductVO> getCabSkuInventory4app(Map<String, Object> params) {
        return baseMapper.getCabSkuInventory(params);
    }

    @Override
    public Page<CellSingleInventoryProductVO> pageList4app(Page page, Map<String, Object> params) {
        List<CellSingleInventoryProductVO> list = baseMapper.list4app(page, params);
        page.setRecords(list);
        return page;
    }

    @Override
    public Page<CellSingleInventoryProductVO> pageList4Alarm(Page page, Map<String, Object> params) {
        List<CabInventoryProduct> inventoryProductList = inventoryProductService.totalQuantityBySkuCode();
        List<CellSingleInventoryProductVO> list = baseMapper.list4Alarm(page, params);
        for (CellSingleInventoryProductVO cellSingleInventoryProductVO : list) {
            for (CabInventoryProduct cabInventoryProduct : inventoryProductList) {
                if(cellSingleInventoryProductVO.getSkuCode().equals(cabInventoryProduct.getSkuCode())){
                    cellSingleInventoryProductVO.setTotalInventory(cabInventoryProduct.getQuantity());
                    cellSingleInventoryProductVO.setTotalInventoryPack(cabInventoryProduct.getQuantityPack());
                }
            }
        }
        page.setRecords(list);
        return page;
    }

    @Override
    public List<CelllnventoryAlarmExportVO> getCelllnventoryAlarmExport(Map<String, Object> params) {
        List<CabInventoryProduct> inventoryProductList = inventoryProductService.totalQuantityBySkuCode();
        List<CelllnventoryAlarmExportVO> list = baseMapper.getCelllnventoryAlarmExport(params);
        for (CelllnventoryAlarmExportVO celllnventoryAlarmExportVO : list) {
            for (CabInventoryProduct cabInventoryProduct : inventoryProductList) {
                if(celllnventoryAlarmExportVO.getSkuCode().equals(cabInventoryProduct.getSkuCode())){
                    celllnventoryAlarmExportVO.setTotalInventory(cabInventoryProduct.getQuantity());
                    celllnventoryAlarmExportVO.setTotalInventoryPack(cabInventoryProduct.getQuantityPack());
                }
            }
        }

        return list;
    }

    @Override
    public List<CellSingleInventoryProductVO> list4Alarm(Map<String, Object> params) {
        return baseMapper.list4Alarm(params);
    }

    @Override
    public Integer count4Alarm(Map<String, Object> params) {
        return baseMapper.count4Alarm(params);
    }

    @Override
    public Page<CellSingleInventoryProductVO> pageListSku4app(Page page, Map<String, Object> params) {
        List<CellSingleInventoryProductVO> list = baseMapper.listSku4app(page, params);
        page.setRecords(list);
        return page;
    }


    @Override
    public Page<CellSingleInventoryProductVO> pageListAlarm4app(Page page, Map<String, Object> params) {
        List<CellSingleInventoryProductVO> list = baseMapper.listAlarm4app(page, params);
        page.setRecords(list);
        return page;
    }

    @Override
    public Page<CellSingleInventoryProductVO> pageList4MainCab(Page page, Map<String, Object> params) {
        List<CellSingleInventoryProductVO> list = baseMapper.list4MainCab(page, params);
        upAlarmThreshold(list);
        page.setRecords(list);
        return page;
    }

    @Override
    public Page<CellSingleInventoryProductVO> pageList4ChildCab(Page page, Map<String, Object> params) {
        List<CellSingleInventoryProductVO> list = baseMapper.list4ChildCab(page, params);
        upAlarmThreshold(list);
        page.setRecords(list);
        return page;
    }

    @Override
    public List<CellSingleInventoryProductVO> listAlarm4app(Map<String, Object> params) {
        return baseMapper.listAlarm4app(params);
    }

    @Override
    public CellSingleInventoryProductVO get4appById(Long id) {
        return baseMapper.get4appById(id);
    }

    @Override
    public Page<CellSingleInventoryProductVO> pageListPressCabinet(Page page, Map<String, Object> params) {
        List<CellSingleInventoryProductVO> list = baseMapper.pageListPressCabinet(page, params);
        page.setRecords(list);
        return page;
    }

    @Override
    public List<CellSingleInventoryProductVO> exportPressCabinet() {
        return baseMapper.exportPressCabinet();
    }

    @Override
    public Page<CellSingleInventoryProductVO> getInventoryBySkuCodeAndBelongCabinet(Page page, Map<String, Object> params) {
        List<CellSingleInventoryProductVO> list = baseMapper.getInventoryBySkuCodeAndBelongCabinet(page, params);
        page.setRecords(list);
        return page;
    }

    @Override
    public List<CellSingleInventoryProductVO> getAlarmInformation() {
        return baseMapper.getAlarmInformation();
    }

    @Override
    public void upAlarmThreshold(List<CellSingleInventoryProductVO> list) {
        if (list.size()>0){
            for (int i = 0; i < list.size(); i++) {
                if (list.get(i).getThresholdBlack()==null || list.get(i).getThresholdBlack()==0){
                    CabAlarmSettings settings = alarmSettingsService.getById(list.get(i).getAlarmId());
                    if (settings!=null ){
                        settings.setThresholdBlack(list.get(i).getAlThresholdBlack());
                        alarmSettingsService.updateById(settings);
                    }
                }
            }
        }

    }


    @Override
    public Integer countSkuByMainCabCode(String mainCabCode) {
        return baseMapper.countSkuByMainCabCode(mainCabCode);
    }

    @Override
    public List<CellSingleInventoryProductExportVO> exportList(Map queryParam) {
        List<CellSingleInventoryProductExportVO> list = baseMapper.exportList(queryParam);
        if (list.size()>0){
            for (int i = 0; i < list.size(); i++) {
                if (list.get(i).getThresholdBlack()==null || list.get(i).getThresholdBlack()==0){
                    CabAlarmSettings settings = alarmSettingsService.getById(list.get(i).getAlarmId());
                    if (settings!=null ){
                        settings.setThresholdBlack(list.get(i).getAlThresholdBlack());
                        alarmSettingsService.updateById(settings);
                    }
                }
            }
        }
        return list;
    }

    @Override
    public List<CellSingleInventoryProductExportVO> exportChild(Map queryParam) {
        List<CellSingleInventoryProductExportVO> list = baseMapper.exportChild(queryParam);
        if (list.size()>0){
            for (int i = 0; i < list.size(); i++) {
                if (list.get(i).getThresholdBlack()==null || list.get(i).getThresholdBlack()==0){
                    CabAlarmSettings settings = alarmSettingsService.getById(list.get(i).getAlarmId());
                    if (settings!=null ){
                        settings.setThresholdBlack(list.get(i).getAlThresholdBlack());
                        alarmSettingsService.updateById(settings);
                    }
                }
            }
        }
        return list;
    }
}
